#include "buff.h"

/**
 * 09/03/12 - Steven Hartin - Original Creation
 */

CBuff::CBuff(const ENTITY_ID &nId, 
             const string &szBuffName, 
             const BUFF_STATE &nBuffState)
    : m_szBuffName(szBuffName),
      m_dTimeTaken(0.0),
      m_bActive(false),
      m_nBuffState(nBuffState),
      m_nId(nId)
{
    /**
     * @param const string &szBuffName The name of the buff to apply.
     * @param const BUFF_STATE &nBuffState The state to apply the buff to
     */
}

const BUFF_STATE CBuff::getBuffState() const
{
    /**
     * @return BUFF_STATE The current state of the buff
     */
    return m_nBuffState;
}

bool CBuff::isActive() const
{
    /**
     * @return bool Whether the buff is currently active on the player
     */
    return m_bActive;
}

void CBuff::setBuffTime(double dTimeTaken)
{
    /**
     * Sets the total time (in milliseconds) that the buff will be active for
     *
     * @param double dTimeTaken The amount of ms that the buff will last
     */
    m_dTimeTaken = dTimeTaken;
}

double CBuff::getTimeRemaining() const
{
    /**
     * Obtains the amount of time the buff has remaining. If the buff is not
     * active, this will return the time the buff lasts for.
     *
     * @return double The amount of time (in ms) the buff has left.
     */
    if (m_bActive)
    {
        return m_dTimeTaken - m_timeBuffStarted.getTimeDifference();
    }
    return m_dTimeTaken;
}

CTimer CBuff::getEndTime() const
{
    /**
     * @return CTimer The time the buff will end.
     */
    if (m_bActive)
    {
        return m_timeBuffStarted + m_dTimeTaken;
    }
    return CTimer() + m_dTimeTaken;
}

bool CBuff::stop(CPlayer *pPlayer)
{
    /**
     * Stops the buff at the currnt moment in time for a specific player.
     *
     * @param CPlayer *pPlayer The player to stop the buff for.
     */
    if (m_bActive)
    {
        if (removeBuff(pPlayer))
        {
            m_bActive = false;
            return true;
        }
    }
    return false;
}

bool CBuff::start(CPlayer *pPlayer)
{
    /**
     * Starts the buff for a specific player This applies the buff and sets
     * the active state.
     *
     * @param CPlayer *pPlayer The player to start the buff for.
     */
    if (! m_bActive)
    {
        if (applyBuff(pPlayer))
        {
            m_bActive = true;
            m_timeBuffStarted = CTimer();
            return true;
        }
    }
    return false; 
}

const string &CBuff::getBuffName() const
{
    /**
     * @return const string& The name of the buff
     */
    return m_szBuffName;
}

const ENTITY_ID CBuff::getId() const
{
    /**
     * @return const ENTITY_ID The Id of the buff.
     */
    return m_nId;
}